package cn.bandeqing.pay;

import cn.bandeqing.base.domain.CurrentWechatMiniAppUser;
import cn.bandeqing.base.resp.Resp;
import cn.bandeqing.pay.gnete.upbc.bean.BeanMock;
import cn.bandeqing.pay.gnete.upbc.config.OpenApiConfig;
import cn.bandeqing.pay.gnete.upbc.message.MerOpenApiRequest;
import cn.bandeqing.pay.gnete.upbc.message.MerOpenApiResponse;
import cn.bandeqing.pay.req.OrderPayReq;
import cn.bandeqing.stp.StpWechatMiniAppUtil;
import com.alibaba.nacos.shaded.com.google.gson.reflect.TypeToken;
import com.gnete.openapi.internal.DefaultOpenApiRequestClient;
import com.gnete.upbc.comn.cashier.rpc.dto.CashierMsgReqDTO;
import com.gnete.upbc.comn.cashier.rpc.dto.CashierMsgRespDTO;
import com.gnete.upbc.comn.rpc.dto.PaymentReqDTO;
import com.gnete.upbc.comn.rpc.dto.PaymentRespDTO;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.validation.Valid;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.lang.reflect.Type;
import java.util.List;

@Tag(name = "支付")
@Slf4j
@RestController
@RequestMapping("/pay")
public class PayController {

    @DubboReference
    private IPayRpc payRpc;

    @PostMapping("/orderDeposit")
    public Resp<Void> orderDeposit(@RequestBody @Valid OrderPayReq req) {

        // 业务请求参数
        PaymentReqDTO<CashierMsgReqDTO> bizReq = BeanMock.getPaymentReqDTO();
        CashierMsgReqDTO msgBody = new CashierMsgReqDTO();
        bizReq.setMsgBody(msgBody);

        msgBody.setMerOrdrNo(BeanMock.getMerOrdrNo(bizReq.getBusiMerNo()));
        msgBody.setTrxTtlAmt("10");

        msgBody.setBizFunc("111011");
        msgBody.setBizType("10600");
        msgBody.setSubject("聚合收银台测试");
        msgBody.setOrdrDesc("聚合收银台测试");
        msgBody.setOprId("001");
        msgBody.setTimeoutExpress("30m");
        msgBody.setFrontRedirectURL("https://www.baidu.com/");

        CurrentWechatMiniAppUser current = StpWechatMiniAppUtil.getCurrent();

        msgBody.setUserNo(current.getCustomerWechatMiniAppOpenId());
        msgBody.setUserType("USER_ID");

        msgBody.setRemark("remark");
        msgBody.setRemark1("remark1");
        msgBody.setRemark2("remark2");
        msgBody.setRemark3("");
        msgBody.setRemark4("");

        msgBody.setTrxTrmInf(BeanMock.getTrxTrmInf());
        msgBody.setRskInf(BeanMock.getRskInf());

        // 01：电脑浏览器(PC)，02：手机或微信浏览器(H5)，03：手机APP(APP)
        msgBody.setTrxChnlType("02");

        msgBody.setSceneInf(BeanMock.getSceneInfo());
//		msgBody.setFqInf(BeanMock.getFqInf());
        msgBody.setGoodsDtl(List.of(BeanMock.getGoodsDtl()));
//		msgBody.setDiscInf(CodeBeanMock.getDiscInf());

        sendGatewayReq(bizReq, "prePay");

        return Resp.success();
    }

    // 发送企业网关请求
    public void sendGatewayReq(PaymentReqDTO<CashierMsgReqDTO> bizReq, String methodName) {
        // 网关请求参数(请求参数可以是json报文、对象)
        MerOpenApiRequest<MerOpenApiResponse> gateWayReq = new MerOpenApiRequest<MerOpenApiResponse>(bizReq);
        gateWayReq.setApiInterfaceId("gnete.upbc.cashier.trade");
        gateWayReq.setApiMethodName(methodName);
        gateWayReq.setApiVersion("1.0.1");
        gateWayReq.setNeedSign(true);
        gateWayReq.setResponseClass(MerOpenApiResponse.class);

        // 发送网关请求
        DefaultOpenApiRequestClient client = OpenApiConfig.getClient();
        client.requestApi(gateWayReq);

//        System.out.println("请求环境: " + OpenApiConfig.serverUrl);
//        System.out.println("上送报文: " + client.getRequestBody());
//        System.out.println("响应报文: " + client.getResponseBody());

        log.info("请求环境 {}", OpenApiConfig.serverUrl);
        log.info("上送报文 {}", client.getRequestBody());
        log.info("响应报文 {}", client.getResponseBody());

        // 响应结果处理
        Type type = new TypeToken<MerOpenApiResponse<PaymentRespDTO<CashierMsgRespDTO>>>() {
        }.getType();
        MerOpenApiResponse<PaymentRespDTO<CashierMsgRespDTO>> resp = BeanMock.gson.fromJson(client.getResponseBody(), type);

        if ("00000".equals(resp.getCode())) {
//            System.out.println("网关调用成功:" + resp.getCode() + " " + resp.getMsg());

            log.info("网关调用成功 {} {}", resp.getCode(), resp.getMsg());

            if ("00000".equals(resp.getResponse().getSubCode())) {
//                System.out.println("业务系统调用成功:" + resp.getCode() + " " + resp.getMsg());

                log.info("业务系统调用成功 {} {}", resp.getCode(), resp.getMsg());

                if ("00000".equals(resp.getResponse().getMsgBody().getRetCode())) {
//                    System.out.println("支付订单处理完成:" + resp.getResponse().getMsgBody().getRetCode() + " " + resp.getResponse().getMsgBody().getRetMsg());

                    log.info("支付订单处理完成 {} {}", resp.getResponse().getMsgBody().getRetCode(), resp.getResponse().getMsgBody().getRetMsg());

                } else {
//                    System.out.println("支付订单处理失败:" + resp.getResponse().getMsgBody().getRetCode() + " " + resp.getResponse().getMsgBody().getRetMsg());

                    log.info("支付订单处理失败 {} {}", resp.getResponse().getMsgBody().getRetCode(), resp.getResponse().getMsgBody().getRetMsg());
                }
            } else {
//                System.out.println("业务系统调用失败:" + resp.getResponse().getSubCode() + " " + resp.getResponse().getSubMsg());

                log.info("业务系统调用失败 {} {}", resp.getResponse().getSubCode(), resp.getResponse().getSubMsg());
            }
        } else {

//            System.out.println("网关调用失败:" + resp.getCode() + " " + resp.getMsg());
            log.info("网关调用失败 {} {}", resp.getCode(), resp.getMsg());
        }
    }

}
